package com.sky.financelake.ui.fragment;

import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.viewpager2.widget.ViewPager2;

import com.google.android.material.tabs.TabLayout;
import com.google.android.material.tabs.TabLayoutMediator;
import com.sky.financelake.R;
import com.sky.financelake.adapter.CommunityTabAdapter;
import com.sky.financelake.base.BaseF;
import com.sky.financelake.databinding.FragmentCommunityBinding;
import com.sky.financelake.ui.fragment.community.BarF;
import com.sky.financelake.ui.fragment.community.NewsF;
import com.sky.financelake.ui.fragment.community.SkillF;
import com.sky.financelake.ui.fragment.community.TalkF;
import com.sky.financelake.utils.LoadingUtil;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class CommunityF extends BaseF {


    private FragmentCommunityBinding binding;
    private LoadingUtil loadingUtil;

    private ViewPager2 viewPager2;
    private TabLayout tabLayout;
    private CommunityTabAdapter adapter;
    private List<Fragment> fragments = new ArrayList<>();
    private String[] tabs;

    private TabLayoutMediator tm;

    public static class SingleHolder {
        private static CommunityF instance = new CommunityF();
    }

    private CommunityF() {
    }

    public static CommunityF newInstance() {
        return SingleHolder.instance;
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        Log.i(TAG, "onCreateView: ");
        binding = FragmentCommunityBinding.inflate(getLayoutInflater());
        loadingUtil = LoadingUtil.newInstance(binding.getRoot());

        tabs = getResources().getStringArray(R.array.community_tabs);

        fragments.add(NewsF.newInstance());
        fragments.add(SkillF.newInstance());
        fragments.add(BarF.newInstance());
        fragments.add(TalkF.newInstance());

        viewPager2 = binding.viewPager2;
        tabLayout = binding.tabs;

        adapter = new CommunityTabAdapter(requireActivity(), fragments);
        viewPager2.setAdapter(adapter);
        viewPager2.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                super.onPageSelected(position);
            }
        });

        tm = new TabLayoutMediator(tabLayout, viewPager2, new TabLayoutMediator.TabConfigurationStrategy() {
            @Override
            public void onConfigureTab(@NonNull TabLayout.Tab tab, int position) {
                tab.setText(tabs[position]);
            }
        });
        tm.attach();


        return binding.getRoot();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        tm.detach();
    }
}
